/**
 * Created by 준영 on 2014-04-11.
 */
var redis = require('redis');

var createRedisClient = function(redisUrl){
    if (redisUrl){
        var rtg  = require("url").parse(redisUrl);
        var redisClient = redis.createClient(rtg.port || 6379, rtg.hostname);
        if (rtg.auth)
        {
            var authString = rtg.auth;
            if (authString.indexOf(':') !== -1) {
                authString = authString.split(":")[1];
            }

            redisClient.auth(authString);
        }

        return redisClient;
    }
    else{
        return redis.createClient();
    }
};

exports.createRedisClient = createRedisClient;

exports.KeyValueStore = (function(){
    var KeyValueStore = function(redisClient){
        this.client = redisClient;
    };

    KeyValueStore.prototype.setAuthRedirectionUrl = function(key, url, callback){
        this.client.set('authRedirectionUrl:' + key, url, callback);
    };

    KeyValueStore.prototype.getAuthRedirectionUrl = function(key, callback){
        this.client.get('authRedirectionUrl:' + key, callback);
    };

    KeyValueStore.prototype.removeAuthRedirectionUrl = function(key, callback){
        this.client.del('authRedirectionUrl:' + key, callback);
    };

    return KeyValueStore;
})();